########################################################################
# util.py - Utility functions that don't fit well elsewhere.
#
# Created 2006.06.08 by Rick Barrett
#
# Copyright (c) 2006 by Pixel Corps.  This program may be used under
# the terms of the X11 license, a copy of which should have accompanied
# this program, in the LICENSE file.
########################################################################

import banner, localparms

import os, wx


def GetBanner(panel, otherSizer):
	image = FindAppResource('upload-header.gif')
	if image:
		return banner.BannerSizer(panel, image,
				otherSizer.GetMinSize().GetWidth(), 0)
	else:
		print 'BANNER: failed to find image'
		bannerText = "%s\nsave your assets" % localparms.parms.programName
		textBox = wx.StaticText(panel, wx.ID_ANY, bannerText,
				style = wx.ALIGN_LEFT)
		sizer = wx.BoxSizer(wx.VERTICAL)
		sizer.Add(textBox, flag = wx.ALIGN_CENTER)
		return sizer



def GetTempPath():
	'''Figure out the best 'temp' directory to use'''
	if 'TEMP' in os.environ:
		return os.environ['TEMP']
	else:
		if os.name == 'mac' or os.name == 'posix':
			return '/tmp'
		else:
			return 'C:/'

def GetExePath():
	if os.name in ("win", "nt"):
		try:
			import win32api
			exeName = win32api.GetModuleFileName(win32api.GetModuleHandle(None))
			return exeName
		except:
			return os.getcwd()
	else:
		return os.getcwd()

def FindAppResource(basename):
	exePath = os.path.dirname(GetExePath())
	for f in (
			basename,							# CWD
			os.path.join('..', basename),		# parent of dev dir
			os.path.join('..', '..', basename),	# development root
			os.path.join('..', '..', 'default', basename),	# repo default
			os.path.join(exePath, basename)):	# executable path
		if os.access(f, os.R_OK):
			return f
	
	return None
